package com.xykj.enterprise.wechat.busi.service.identity;

import com.xykj.enterprise.wechat.core.dao.JsapiTicketDao;
import com.xykj.enterprise.wechat.core.model.dodb.JsapiTicket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author george
 * @create 2021-04-19 21:04
 */
@Service
public class JsapiTicketServiceImpl implements JsapiTicketService {
    @Autowired
    private JsapiTicketDao jsapiTicketDao;

    @Override
    public JsapiTicket getAgentTicket(String corpid, String agentId) {
        JsapiTicket ticket = jsapiTicketDao.get(corpid, agentId);
        if (ticket == null) {
            return null;
        }
        if (ticket.isAgentTicketExpire()) {
            return null;
        }
        return ticket;
    }

    @Override
    public JsapiTicket getCorpTicket(String corpid, String agentId) {
        JsapiTicket ticket = jsapiTicketDao.get(corpid, agentId);
        if (ticket == null) {
            return null;
        }
        if (ticket.isCorpTicketExpire()) {
            return null;
        }
        return ticket;
    }

    @Override
    public void saveAgentTicket(String corpid, String agentId, String jsapTicket, Integer expire) {
        JsapiTicket ticket = jsapiTicketDao.get(corpid, agentId);
        if (ticket == null) {
            jsapiTicketDao.saveAgentTicket(corpid, agentId, jsapTicket, expire);
        } else {
            jsapiTicketDao.updateAgentTicket(ticket.getId(), corpid, agentId, jsapTicket, expire);
        }
    }

    @Override
    public void saveCorpTicket(String corpid, String agentId, String jsapTicket, Integer expire) {
        JsapiTicket ticket = jsapiTicketDao.get(corpid, agentId);
        if (ticket == null) {
            jsapiTicketDao.saveCorpTicket(corpid, agentId, jsapTicket, expire);
        } else {
            jsapiTicketDao.updateCorpTicket(ticket.getId(), corpid, agentId, jsapTicket, expire);
        }
    }
}
